#include <iostream>

using namespace std;
typedef unsigned long NUM;

int main()
{
	const NUM n = 1000000;
	NUM result = 0, i, j, *a = new NUM[n+1];

    for(a[1]=0, i=2;i<=n;i++)
        a[i]=1;
    for (i=2;i<=n/2;i++)
        for (j=2; j<=n/i;j++)
            a[i*j]=0;


    for(i=2;i<=n;i++)
    {
        if(a[i])
        {
        	if(i > 100000)
        	{
        		NUM t1 = i/10+(i%10)*100000;
        		NUM t2 = t1/10+(t1%10)*100000;
        		NUM t3 = t2/10+(t2%10)*100000;
        		NUM t4 = t3/10+(t3%10)*100000;
        		NUM t5 = t4/10+(t4%10)*100000;
        		if(a[t1] && a[t2] && a[t3] && a[t4] && a[t5])
        			result += 6;
        		a[t1] = 0;
        		a[t2] = 0;
        		a[t3] = 0;
        		a[t4] = 0;
				a[t5] = 0;
        	}
        	else if(i > 10000)
        	{
        		NUM t1 = i/10+(i%10)*10000;
        		NUM t2 = t1/10+(t1%10)*10000;
        		NUM t3 = t2/10+(t2%10)*10000;
        		NUM t4 = t3/10+(t3%10)*10000;
        		if(a[t1] && a[t2] && a[t3] && a[t4])
        			result += 5;
        		a[t1] = 0;
        		a[t2] = 0;
        		a[t3] = 0;
        		a[t4] = 0;
        	}
        	else if(i > 1000)
        	{
        		NUM t1 = i/10+(i%10)*1000;
        		NUM t2 = t1/10+(t1%10)*1000;
        		NUM t3 = t2/10+(t2%10)*1000;
        		if(a[t1] && a[t2] && a[t3])
        			result += 4;
        		a[t1] = 0;
        		a[t2] = 0;
        		a[t3] = 0;
        	}
        	else if(i > 100)
        	{
        		NUM t1 = i/10+(i%10)*100;
        		NUM t2 = t1/10+(t1%10)*100;
        		if(a[t1] && a[t2])
        			result += 3;
        		a[t1] = 0;
        		a[t2] = 0;
        	}
        	else if(i > 10)
        	{
        		NUM t = i/10+(i%10)*10;
        		if(a[t])
        		{
        			if(t == i) result++;
        			else result += 2;
        		}
        		a[t] = 0;
        	}
        	else
        	{
        		result++;
        	}

        }
    }
	cout << endl << result  << endl;
	return 0;
}



/*
The number, 197, is called a circular prime because all rotations of the digits: 197, 971, and 719, are themselves prime.
There are thirteen such primes below 100: 2, 3, 5, 7, 11, 13, 17, 31, 37, 71, 73, 79, and 97.
How many circular primes are there below one million?
*/
